﻿Public Class frmRolesDescuentos

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim oFD As New OpenFileDialog
        With oFD
            .Title = "Seleccionar fichero"
            .Filter = "Ficheros de texto (*.xls;*.xlsx)|*.xls;*.xlsx" & _
                      "|Todos los ficheros (*.*)|*.*"
            .FileName = Me.txtFile.Text
            If .ShowDialog = System.Windows.Forms.DialogResult.OK Then
                Me.txtFile.Text = .FileName
            End If
        End With
        Excel()
        MsgBox("Ahora puede importar el archivo", MsgBoxStyle.Information, "Aviso")
    End Sub
    Private Sub Excel()

        dgRoles.Rows.Clear()
        Application.DoEvents()

        Dim xl As Object
        Dim wb As Object

        'Crear objeto excel
        xl = CreateObject("Excel.Application")
        xl.Visible = False
        wb = xl.activeworkbook
        wb = xl.Workbooks.Open _
        (txtFile.Text, True, True, , "")

        Dim rng As Integer = 1
        Dim ROL As String = ""
        Dim Nombre As String
        Dim HoraInicio As String
        Dim HoraTermino As String
        Dim str() As String
        Dim Descuento As String
        Dim Dia As String


        For i = 3 To 100000
            ROL = wb.sheets(1).cells(i, 2).value
            If ROL = "" Then
                Exit For
            End If
            Nombre = wb.sheets(1).cells(i, 3).value & " " & wb.sheets(1).cells(i, 2).value
            str = Split(wb.sheets(1).cells(i, 3).value, "-", 2)
            HoraInicio = str(0)
            HoraTermino = str(1)
            Descuento = wb.sheets(1).cells(i, 4).value
            Dia = wb.sheets(1).cells(i, 5).value
            dgRoles.Rows.Add(Nombre, HoraInicio, HoraTermino, 0, 0, Descuento, 50, Dia)
        Next

        'cerramos el archivo Excel
        wb.Close(SaveChanges:=False)
        xl.Quit()
    End Sub

    Private Sub frmRolesDescuentos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        txtFile.Clear()
        dgRoles.Rows.Clear()

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim Rol As New clsRolesDescuentos

        For i = 0 To dgRoles.Rows.Count - 1
            Rol.Nombre = dgRoles.Rows(i).Cells(0).Value
            Rol.Origen = ""
            Rol.Destino = ""
            Rol.HoraInicio = dgRoles.Rows(i).Cells(1).Value
            Rol.HoraTermino = dgRoles.Rows(i).Cells(2).Value
            Rol.TipoDescuento = 0
            Rol.Marcas = 0
            Rol.PorcentajeMarcas = dgRoles.Rows(i).Cells(5).Value
            Rol.PorcentajeDescuento = dgRoles.Rows(i).Cells(6).Value
            Rol.TipoDia = dgRoles.Rows(i).Cells(7).Value
            Rol.Insertar()
        Next
        frmCatalogoRoles.CargaRoles()
        Close()

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
End Class